package com.amit.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class CHibernateTemplate {

	public static void save(Candidate can) {
		

		try
		{
			SessionFactory sessionfactory=ChibernateUtil.getfactory();
			Session session=sessionfactory.openSession();
			Transaction tx=session.beginTransaction();
			session.save(can);
			tx.commit();
			session.close();
		}
		catch (Exception e) {
			e.printStackTrace();
			
			
		}
		
		
	}

	public static Candidate load(Class<Candidate> class1, int i) {
		
		Candidate can=null;
		
		try
		{
			SessionFactory sessionfactory=ChibernateUtil.getfactory();
			Session session=sessionfactory.openSession();
			Transaction tx=session.beginTransaction();
		 can=(Candidate) session.load(class1, i);
			tx.commit();
			session.close();
		}
		catch (Exception e) {
			e.printStackTrace();
			
			
		}
		return  can;
	}

	public static void delete(Class<Candidate> class1, int i) {
		
		
		try
		{
			SessionFactory sessionfactory=ChibernateUtil.getfactory();
			Session session=sessionfactory.openSession();
			Transaction tx=session.beginTransaction();
			Object o=session.load(class1, i);
			 session.delete(o);
			tx.commit();
			session.close();
		}
		catch (Exception e) {
			e.printStackTrace();
			
			
		}
		
	}

	public static void update(Candidate can) {
		try
		{
			SessionFactory sessionfactory=ChibernateUtil.getfactory();
			Session session=sessionfactory.openSession();
			Transaction tx=session.beginTransaction();
			session.update(can);
			
			tx.commit();
			session.close();
		}
		catch (Exception e) {
			e.printStackTrace();
			
			
		}
	}

}
